package com.vision.world.survey.presenter;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ProgressBar;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.vision.world.survey.Interface.OnTouchMapEvent;
import com.vision.world.survey.Interface.view.IMainView;
import com.vision.world.survey.R;
import com.vision.world.survey.adapters.SearchResultAdapter;
import com.vision.world.survey.managers.LayerManager;
import com.vision.world.survey.managers.QueryManager;
import com.vision.world.survey.util.CommonUtil;
import com.vision.world.survey.util.MapTools;

import java.io.IOException;

/**
 * Created by Administrator on 2017/10/13.
 */

public class MainPresenter {
    private Context mContext;
    private IMainView mMainView;
    private SearchResultAdapter mSearchResultAdapter=null;
    public MainPresenter(Context context,IMainView mainView){
        mContext=context;
        mMainView=mainView;
    }

    public SearchResultAdapter getSearchResultAdapter(){
        if (mSearchResultAdapter==null)
        {
            mSearchResultAdapter=new SearchResultAdapter(mContext);
        }
        return mSearchResultAdapter;
    }


    public void search(final String queryKey, final MapView mMapView){
        String strQueryField = mContext.getResources().getString(R.string.query_field);
        QueryManager.getInstance().initQuaryByField(mContext,mMapView,strQueryField, queryKey,new OnTouchMapEvent(){
            @Override
            public void refreshViewOnStartSearch(String info) {
                CommonUtil.showProgressDialog(mContext,info);
            }

            @Override
            public void refreshViewOnStartSearch(MotionEvent e) {
                // show progressDialog
                CommonUtil.showProgressDialog(mContext,mContext.getResources().getString(R.string.search_by_field));
            }

            @Override
            public void refreshViewOnSearchSuccess(String info, int count) {
                CommonUtil.dismissProgressDialog();
//                CommonUtil.showMessage(mContext,info);
                mSearchResultAdapter.setItem(QueryManager.getInstance().SearchResultFromOperationLayer);
                mSearchResultAdapter.notifyDataSetChanged();
                mMainView.showSearchViewResult(queryKey,info, count);
            }

            @Override
            public void refreshViewOnSearchFailed(String info, Exception ex) {
                CommonUtil.dismissProgressDialog();
                CommonUtil.showMessage(mContext,info);
            }
        });
    }

    public void initMap(Context context, final MapView mMapView, ProgressBar progressBar){
        LayerManager mLayerManager= LayerManager.getInstance();
        mLayerManager.iniBaseMap(context,mMapView);
        mLayerManager.initGraphicOverlay(mMapView);
        mLayerManager.initLayerOperatinal(context,mMapView);
        //设置地图显示范围
        mLayerManager.changeViewPoint(mMapView);

        //地图刷新进度条
        MapTools.initDrawStatusChanged(context,mMapView,progressBar,new OnTouchMapEvent() {
            @Override
            public void refreshViewOnStartSearch(String info) {
                //显示查询滚动条
                CommonUtil.isIdentify=true;
                Log.i("isFreshIdentify","mapFresh refreshViewOnStartSearch   isIdentify="+CommonUtil.isIdentify);
                CommonUtil.showProgressDialog(mContext,info);
                //关闭结果清单
//                mFeatureCoverFlow.setVisibility(View.GONE);
                //yi ben zheng jing de shuo dao ziwei xiang
            }

            @Override
            public void refreshViewOnStartSearch(MotionEvent e) {

            }

            @Override
            public void refreshViewOnSearchSuccess(String info, int count) {
                if(QueryManager.getInstance().isSelectByGeometryComplete()) {
                    //如果所有图层均完成搜索
                    LocationDisplay locationDisplay = mMapView.getLocationDisplay();
                    if(!locationDisplay.isStarted()){
                        locationDisplay.startAsync();
                    }
                    // get the location about the device
                    Point pointMyLocation =  locationDisplay.getMapLocation();
                    try {
                        QueryManager.getInstance().disSort(pointMyLocation);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    CommonUtil.showMessage(mContext, info);
                    Log.i("GPSBoundary", "查询成功，查到结果数量：" +count + "条");
                    mMainView.shouBoundaryResult();
                    CommonUtil.dismissProgressDialog();
                }
                CommonUtil.isIdentify=false;
                Log.i("isFreshIdentify","mapFresh refreshViewOnSearchSuccess  isIdentify="+CommonUtil.isIdentify);
            }

            @Override
            public void refreshViewOnSearchFailed(String info, Exception ex) {
                CommonUtil.showMessage(mContext,info+":\n"+ex.getMessage());
                Log.w("GPSBoundary",ex.getMessage());
                if(QueryManager.getInstance().isSelectByGeometryComplete()) {
                    //如果所有图层均完成搜索，关闭查询进度条
                    CommonUtil.dismissProgressDialog();
                }
                CommonUtil.isIdentify=false;
                Log.i("isFreshIdentify","mapFresh refreshViewOnSearchFailed  isIdentify="+CommonUtil.isIdentify);
            }
        });
    }

    public  void selectByLocationBoundary(final MapView mapView){
        //清除map中的目标点（如果有的话）
        CommonUtil.screenNo=0;
        //todo 清空地图上绘已经绘制的graphic要素
        QueryManager.getInstance().clearGraphicOverlay(mapView);
        QueryManager.getInstance().initSelectByLocationBoundary(mContext,mapView, new OnTouchMapEvent() {
            @Override
            public void refreshViewOnStartSearch(String info) {
                //显示查询滚动条
                CommonUtil.isIdentify=true;
                Log.i("isFreshIdentify","identify refreshViewOnStartSearch  isIdentify="+CommonUtil.isIdentify);
                CommonUtil.showProgressDialog(mContext,info);
                //关闭结果清单
//                mFeatureCoverFlow.setVisibility(View.GONE);
                //yi ben zheng jing de shuo dao ziwei xiang
            }

            @Override
            public void refreshViewOnStartSearch(MotionEvent e) {

            }

            @Override
            public void refreshViewOnSearchSuccess(String info, int count) {
                if(QueryManager.getInstance().isSelectByGeometryComplete()) {
                    //如果所有图层均完成搜索
                    LocationDisplay locationDisplay = mapView.getLocationDisplay();
                    if(!locationDisplay.isStarted()){
                        locationDisplay.startAsync();
                    }
                    // get the location about the device
                    Point pointMyLocation =  locationDisplay.getMapLocation();
                    try {
                        QueryManager.getInstance().disSort(pointMyLocation);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    CommonUtil.showMessage(mContext, info);
                    Log.i("GPSBoundary", "查询成功，查到结果数量：" +count + "条");
                    CommonUtil.screenCount = count % CommonUtil.NUMBER_PER_SCREEN == 0 ?
                            count / CommonUtil.NUMBER_PER_SCREEN :
                            count/ CommonUtil.NUMBER_PER_SCREEN + 1;

                    mMainView.shouBoundaryResult();
                    //int index
                    CommonUtil.dismissProgressDialog();
                }
                //
                CommonUtil.isIdentify=false;
                Log.i("isFreshIdentify","identify refreshViewOnSearchSuccess  isIdentify="+CommonUtil.isIdentify);
            }

            @Override
            public void refreshViewOnSearchFailed(String info, Exception ex) {
                CommonUtil.showMessage(mContext,info+":\n"+ex.getMessage());
                Log.w("GPSBoundary",ex.getMessage());
                if(QueryManager.getInstance().isSelectByGeometryComplete()) {
                    //如果所有图层均完成搜索，关闭查询进度条
                    CommonUtil.dismissProgressDialog();
                }
                CommonUtil.isIdentify=false;
                Log.i("isFreshIdentify","identify refreshViewOnSearchFailed  isIdentify="+CommonUtil.isIdentify);
            }
        });
    }

}
